package com.zxy.express.service.impl;

import com.autopai.base.common.mybatis.service.impl.BaseServiceImpl;
import com.autopai.base.common.web.constant.Constant;
import com.autopai.base.common.web.convert.ConvertUtils;
import com.autopai.base.common.web.page.PageData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxy.express.dao.ReciteMaterialDao;
import com.zxy.express.dao.SummaryMaterialDao;
import com.zxy.express.dto.ReciteMaterialDTO;
import com.zxy.express.dto.SummaryMaterialDTO;
import com.zxy.express.entity.ReciteMaterialEntity;
import com.zxy.express.entity.SummaryMaterialEntity;
import com.zxy.express.service.ReciteMaterialService;
import com.zxy.express.service.SummaryMaterialService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 练习素材表
 *
 * @author zhangxiying ${email}
 * @since ${version} 2025-07-01
 */
@Service
public class SummaryMaterialServiceImpl extends BaseServiceImpl<SummaryMaterialDao, SummaryMaterialEntity> implements SummaryMaterialService {

    @Override
    public PageData<SummaryMaterialDTO> page(Map<String, Object> params) {
        IPage<SummaryMaterialEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );

        return getPageData(page, SummaryMaterialDTO.class);
    }

    @Override
    public List<SummaryMaterialDTO> list(Map<String, Object> params) {
        List<SummaryMaterialEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, SummaryMaterialDTO.class);
    }

    private QueryWrapper<SummaryMaterialEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SummaryMaterialEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        return wrapper;
    }

    @Override
    public SummaryMaterialDTO get(String id) {
        SummaryMaterialEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, SummaryMaterialDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SummaryMaterialDTO dto) {
        SummaryMaterialEntity entity = ConvertUtils.sourceToTarget(dto, SummaryMaterialEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SummaryMaterialDTO dto) {
        SummaryMaterialEntity entity = ConvertUtils.sourceToTarget(dto, SummaryMaterialEntity.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        //逻辑删除
        //logicDelete(ids, MaterialEntity.class);

        //物理删除
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }

}